package com.mz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mz.common.dto.GlobalException;
import com.mz.common.dto.PaginationVo;
import com.mz.dao.SmsBannerDao;
import com.mz.entity.SmsBanner;
import com.mz.entity.vo.BannerVo;
import com.mz.service.SmsBannerService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (SmsBanner)表服务实现类
 *
 * @author makejava
 * @since 2023-12-13 13:38:32
 */
@Service
public class SmsBannerServiceImpl  implements SmsBannerService {

    @Resource
    private SmsBannerDao smsBannerDao;

    @Override
    public void addBanner(BannerVo bannerVo) throws Exception {

        SmsBanner smsBanner = new SmsBanner();
        BeanUtils.copyProperties( bannerVo, smsBanner );
        smsBanner.setId(null);
        smsBannerDao.insert( smsBanner );
        return;
    }

    @Override
    public IPage getBannerPagination(PaginationVo paginationVo) throws Exception {

        IPage page = new Page( paginationVo.getPageIndex(), paginationVo.getPageSize() );
        LambdaQueryWrapper<SmsBanner> lqs = new LambdaQueryWrapper<>();
        lqs.orderByDesc(SmsBanner::getCreateTime);
        smsBannerDao.selectPage(page, lqs);
        return page;
    }

    @Override
    public void removeBanner(List<Long> idList) throws Exception {
        if( idList == null || idList.size() == 0 ){
            throw GlobalException.gen(10014, "轮播图id不存在");
        }
        smsBannerDao.deleteBatchIds(idList);
        return;
    }

    @Override
    public void updateBanner(BannerVo bannerVo) throws Exception {

        if( bannerVo.getId() == null ){
            throw GlobalException.gen(10030, "轮播图id不能为空");
        }
        SmsBanner smsBanner = new SmsBanner();
        BeanUtils.copyProperties( bannerVo, smsBanner );
        smsBannerDao.updateById(smsBanner);
        return;
    }
}

